package com.example.musicplayer.musicTools;

import android.content.res.Resources;

import com.example.musicplayer.DataLoadManager;
import com.example.musicplayer.MusicPlayerApplication;
import com.example.musicplayer.R;
import com.example.musicplayer.commonUtils.ChineseUtil;
import com.example.musicplayer.commonUtils.DisplayUtil;
import com.example.musicplayer.musicClass.MusicInfo;
import com.example.musicplayer.settings.MusicPlayerSettings;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Lyric {
    //record the lyric text size
    public static final int LYRIC_SIZE_1 = 1;
    public static final int LYRIC_SIZE_2 = 2;
    public static final int LYRIC_SIZE_3 = 3;
    public static final int LYRIC_SIZE_4 = 4;
    public static final int LYRIC_SIZE_5 = 5;
    private static int sLyricSize = -1;
    //表示用简体字
    public static final int SIMPLIFIED = 0;
    //表示用繁体字
    public static final int TRADITIONAL = 1;
    private static List<String> lyrics;
    private static List<Integer> lyricTime;
    private static int sCurrentMusicId;

    public static MusicDetailInfo get(MusicInfo musicInfo) {
        if (musicInfo == null) {
            return null;
        }
        MusicDetailInfo music = new MusicDetailInfo();
        music.mId = musicInfo.getId();
        music.mTitle = musicInfo.getTitle();
        music.mSinger = musicInfo.getArtist();
        music.mAlbum = musicInfo.getAlbum();
        String path = musicInfo.getLyricPath();
        BufferedReader bw;
        //get the reader
        try {
            bw = new BufferedReader(new InputStreamReader(new FileInputStream(path), "GBK"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        String str = null;
        try {
            while ((str = bw.readLine()) != null) {
                get(music, str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return music;
    }

    private static void get(MusicDetailInfo music, String string) {
        int index = string.lastIndexOf("]");
        String temp = string.substring(index + 1);
        if (temp.length() > 0) {
            music.mLyrics.add(temp);
            int endIndex = temp.indexOf(":");
            if (endIndex == -1) {
                endIndex = temp.indexOf("：");
            }
            if (endIndex > 0) {
                getDetailInfo(music, temp, endIndex);
            }
        }
    }

    private static void getDetailInfo(MusicDetailInfo music, String string, int ednIndex) {
        String prefix = string.substring(0, ednIndex).toLowerCase().trim();
        String suffix = string.substring(ednIndex + 1).trim();
        switch (prefix) {
            case "词":
            case "lyrics by":
                music.mLyric = suffix;
                break;
            case "曲":
            case "composed by":
                music.mComposed = suffix;
                break;
            case "编":
            case "arranged by":
                music.mArranged = suffix;
                break;
            case "制作人":
            case "produced by":
                music.mProduced = suffix;
                break;
        }
        if (prefix.contains("作词")) {
            music.mLyric = suffix;
        }
        if (prefix.contains("作曲")) {
            music.mComposed = suffix;
        }
        if (prefix.contains("编曲")) {
            music.mArranged = suffix;
        }
        if (prefix.contains("制作人")) {
            music.mProduced = suffix;
        }
    }

    //给定路径构建歌词
    public static boolean create(String lyricPath, Resources resources, int musicId) {
        lyrics = new ArrayList<String>();
        lyricTime = new ArrayList<Integer>();
        sCurrentMusicId = musicId;
        BufferedReader bw;
        try {
            bw = new BufferedReader(new InputStreamReader(new FileInputStream(lyricPath), "GBK"));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        String str = null;
        try {
            while ((str = bw.readLine()) != null) {
                checkLyric(str, resources);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    //处理每次读进来的字符串
    private static void checkLyric(String string, Resources resources) {
        int index = string.lastIndexOf("]");
        String string1 = string.substring(0, index + 1);
        String string2 = string.substring(index + 1);
        if (string1.equals("[00:00:00]")) {
            addLyric(resources.getString(R.string.music_lyric_empty));
            lyricTime.add(0);
        } else if (string2.length() > 0) {
            addLyric(string2);
            lyricTime.add(convertToTime(string1));
        }
    }

    //把歌词文件的时间戳转换成毫秒数
    private static int convertToTime(String timeStamp) {
        try {
            int minute = Integer.parseInt(timeStamp.substring(1, 3));
            int second = Integer.parseInt(timeStamp.substring(4, 6));
            int millisecond = Integer.parseInt(timeStamp.substring(7, 9));
            return minute * 60000 + second * 1000 + millisecond * 10;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    //得到存放歌词的数组的大小
    public static int getSize() {
        return lyrics == null ? 0 : lyrics.size();
    }

    //得到指定位置的歌词
    public static String getLyric(int index) {
        String lyric = "";
        if (index >= 0 && index < lyrics.size()) {
            lyric = lyrics.get(index);
        }
        return lyric;
    }

    //得到指定索引歌词的开始时间
    public static int getStartTime(int position) {
        int startTime = Integer.MAX_VALUE;
        if (position >= 0 && position < lyricTime.size()) {
            startTime = lyricTime.get(position);
        }
        return startTime;
    }

    public static int getCurrentMusicId() {
        return sCurrentMusicId;
    }

    //简体繁体格式转换
    public static void changeChinese() {
        if (MusicPlayerSettings.getLyricChinese() == SIMPLIFIED) {
            MusicPlayerSettings.setLyricChinese(TRADITIONAL);
            changeToTraditional();
        } else if (MusicPlayerSettings.getLyricChinese() == TRADITIONAL) {
            MusicPlayerSettings.setLyricChinese(SIMPLIFIED);
            changeToSimplified();
        }
    }

    //歌词简转繁
    private static void changeToTraditional() {
        for (int i = 0; i < lyrics.size(); i++) {
            String string = lyrics.get(i);
            string = ChineseUtil.J2F(string);
            lyrics.set(i, string);
        }
    }

    //歌词繁转简
    private static void changeToSimplified() {
        for (int i = 0; i < lyrics.size(); i++) {
            String string = lyrics.get(i);
            string = ChineseUtil.F2J(string);
            lyrics.set(i, string);
        }
    }

    //初始化歌词并添加歌词时，判断显示的字体样式
    private static void addLyric(String string) {
        if (MusicPlayerSettings.getLyricChinese() == TRADITIONAL) {
            string = ChineseUtil.J2F(string);
        }
        lyrics.add(string);
    }

    public static int getLyricTextSize(boolean isSelected) {
        if (sLyricSize == -1 && DataLoadManager.getInstance() != null) {
            sLyricSize = DataLoadManager.getInstance().readOneBaseData(
                    DataLoadManager.LYRIC_TEXT_SIZE, LYRIC_SIZE_3);
        }
        int sizeId;
        switch (sLyricSize) {
            case LYRIC_SIZE_1:
                sizeId = R.dimen.lyric_size_1;
                break;
            case LYRIC_SIZE_2:
                sizeId = R.dimen.lyric_size_2;
                break;
            case LYRIC_SIZE_4:
                sizeId = R.dimen.lyric_size_4;
                break;
            case LYRIC_SIZE_5:
                sizeId = R.dimen.lyric_size_5;
                break;
            case LYRIC_SIZE_3:
            default:
                sizeId = R.dimen.lyric_size_3;
                break;
        }
        int textSize = MusicPlayerApplication.getInstance().getResources().getDimensionPixelSize(sizeId);
        //px change to sp
        textSize = DisplayUtil.px2sp(MusicPlayerApplication.getInstance(), textSize);
        if (isSelected) {
            textSize += 3;
        }
        return textSize;
    }

    public static int getLyricSize() {
        if (sLyricSize == -1 && DataLoadManager.getInstance() != null) {
            sLyricSize = DataLoadManager.getInstance().readOneBaseData(
                    DataLoadManager.LYRIC_TEXT_SIZE, LYRIC_SIZE_3);
        }
        return sLyricSize;
    }

    public static void setLyricSize(int size) {
        sLyricSize = size;
        DataLoadManager manager = DataLoadManager.getInstance();
        if (manager != null) {
            manager.writeOneBaseData(DataLoadManager.LYRIC_TEXT_SIZE, sLyricSize);
        }
    }

    public static class MusicDetailInfo {
        public int mId;
        public String mTitle;
        public String mSinger;
        public String mLyric;
        public String mComposed;
        public String mArranged;
        public String mProduced;
        public String mAlbum;
        public List<String> mLyrics;

        public MusicDetailInfo() {
            mId = -1;
            mTitle = "";
            mSinger = "";
            mLyric = "";
            mComposed = "";
            mArranged = "";
            mProduced = "";
            mAlbum = "";
            mLyrics = new ArrayList<String>();
        }
    }
}